package exportExcel;

import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import poi.ExCell;
import poi.ExSheet;
import poi.SampleExcelBuilder;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * Created by Administrator on 2017/10/23.
 */
public class SearchExcelBuilder extends SampleExcelBuilder {
    private SXSSFWorkbook currentWb;

    /**
     * drawExcel
     *
     * @param path    path
     * @param factory factory
     */
    public void drawExcel(String path, DetectExcelWorker factory) {
        currentWb = factory.getCurrentWorkbook();
        OutputStream out = null;
        try {
            out = new FileOutputStream(path);
            drawSummarySheet(currentWb, factory.getSheets().get(0));
            currentWb.write(out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    private void drawSummarySheet(SXSSFWorkbook workbook, ExSheet exSheet) {
        String name = exSheet.getName();
        SXSSFSheet sheet = workbook.createSheet(name);
        List<ExCell> exCells = exSheet.getExCells();

        //Merge Cell
        //set columnWidth
        final int columnWidth10 = 10;
        final int columnWidth5 = 5;
        final int columnWidth25 = 25;
        sheet.setDefaultColumnWidth(columnWidth25);
//        sheet.setColumnWidth(0, columnWidth5 * 256);
//        sheet.setColumnWidth(1, columnWidth25 * 256);
        drawSheet(exCells, sheet, workbook);
        try {
            sheet.flushRows();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
